package com.visikard.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.GetTransactionHistoryRecordPunchKardTask;
import com.visikard.asynctask.GetTransactionHistoryRewardPunchKardTask;
import com.visikard.item.ItemTransactionHistory;
import com.visikard.item.ItemTransactionRewardPunchKard;
import com.visikard.ui.R;
import com.visikard.ui.adapter.TransactionHistoryRecordPunchAdapter;
import com.visikard.ui.adapter.TransactionHistoryRewardPunchAdapter;
import com.visikard.widget.expandedlistview.ExpandingListView;

public class TransactionPunchKardFragment extends BaseFragment
// implements ViewClickable
// , SetTimeParent
{

	private enum ScreenType {
		RecordPunch, RewardPunch;
	}

	// private TextView headerDateBack;
	// private TextView headerDateNext;

	private int pageIndexRecord = 1;
	private boolean isNoLoadMoreRecord = false;
	private boolean isLoadingMoreRecord = false;

	private int pageIndexReward = 1;
	private boolean isNoLoadMoreReward = false;
	private boolean isLoadingMoreReward = false;

	private Context mContext;

	// private static long TIME_MIN = 0;
	// private static long TIME_MAX = 0;

	private ArrayList<ItemTransactionHistory> listItemTransaction;
	TransactionHistoryRecordPunchAdapter transactionAdapter;

	private ExpandingListView recordPunchlistView;

	private View lyRewardContainer;
	private TextView txtGiftKardRewarded, txtGiftKardRedeemed;
	private Button btnRecordPunch, btnRewardPunch;
	private ListView rewardPunchListView;
	private ArrayList<ItemTransactionRewardPunchKard> listItemRewardPunch;
	private TransactionHistoryRewardPunchAdapter mRewardPunchAdapter;

	private ScreenType mScreenType;
	private boolean mFirstLoadRecordPunch, mFirstLoadRewardPunch = false;

	private BaseTask mAsyncTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.transaction_punch_kard_fragment, container, false);

		mContext = getActivity();

		// TIME_MAX = Calendar.getInstance().getTimeInMillis();
		// TIME_MIN = TIME_MAX;

		init(view);

		return view;
	}

	private void init(View view) {
		btnRecordPunch = (Button) view.findViewById(R.id.btn_punch_record);
		btnRewardPunch = (Button) view.findViewById(R.id.btn_punch_reward);
		btnRecordPunch.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mScreenType = ScreenType.RecordPunch;
				updateScreen();
			}
		});
		btnRewardPunch.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mScreenType = ScreenType.RewardPunch;
				updateScreen();
			}
		});

		lyRewardContainer = view.findViewById(R.id.ly_reward_container);
		txtGiftKardRewarded = (TextView) view.findViewById(R.id.txt_nb_rewarded);
		txtGiftKardRedeemed = (TextView) view.findViewById(R.id.txt_nb_redeemed);

		rewardPunchListView = (ListView) view.findViewById(R.id.list_reward_punch_kard);
		rewardPunchListView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				// For load more
				if (totalItemCount > 2 && firstVisibleItem + visibleItemCount >= totalItemCount && !isLoadingMoreReward
						&& !isNoLoadMoreReward) {
					pageIndexRecord++;
					isLoadingMoreReward = true;
					mDataloadMore();
					// load more
				}
			}
		});

		recordPunchlistView = (ExpandingListView) view.findViewById(R.id.list_record_punch_kard);
		recordPunchlistView.setVisibility(View.GONE);

		recordPunchlistView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				// For load more
				if (totalItemCount > 2 && firstVisibleItem + visibleItemCount >= totalItemCount && !isLoadingMoreRecord
						&& !isNoLoadMoreRecord) {
					pageIndexRecord++;
					isLoadingMoreRecord = true;
					mDataloadMore();
					// load more
				}
			}
		});

		mScreenType = ScreenType.RecordPunch;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		updateScreen();
	}

	private void updateScreen() {
		switch (mScreenType) {
			case RecordPunch:
				// disable button record
				btnRecordPunch.setEnabled(false);
				// enable button reward
				btnRewardPunch.setEnabled(true);
				// hide reward part
				lyRewardContainer.setVisibility(View.GONE);
				// show record list
				recordPunchlistView.setVisibility(View.VISIBLE);

				if (!mFirstLoadRecordPunch) {
					getListTrasaction();
					mFirstLoadRecordPunch = true;
				}
				break;
			case RewardPunch:
				// enable button record
				btnRecordPunch.setEnabled(true);
				// disable button reward
				btnRewardPunch.setEnabled(false);
				// hide record list
				recordPunchlistView.setVisibility(View.GONE);
				// show reward part
				lyRewardContainer.setVisibility(View.VISIBLE);

				if (!mFirstLoadRewardPunch) {
					getListTrasaction();
					mFirstLoadRewardPunch = true;
				}
				break;
		}
	}

	@Override
	public void onPause() {
		// ((TransactionHistoryActivity) getActivity()).dismissProgressDialog();

		// if (mDataloadTransactions != null)
		// TaskExecuter.getInstance(getActivity()).cancel(
		// mDataloadTransactions);
		// if (mDataloadMoreTransactions != null)
		// TaskExecuter.getInstance(getActivity()).cancel(
		// mDataloadMoreTransactions);

		super.onPause();
	}

	private void getListTrasaction() {
		switch (mScreenType) {
			case RecordPunch:
				mAsyncTask = new GetTransactionHistoryRecordPunchKardTask(mHandler);
				mAsyncTask.execute(VKRegApplication.uid, Constant.BOOLEAN_TRUE, pageIndexRecord,
						VKRegApplication.mPageSize);
				break;
			case RewardPunch:
				mAsyncTask = new GetTransactionHistoryRewardPunchKardTask(mHandler);
				mAsyncTask.execute(VKRegApplication.uid, pageIndexReward, VKRegApplication.mPageSize);
				break;
		}
	}

	private void mDataloadMore() {
		switch (mScreenType) {
			case RecordPunch:
				mAsyncTask = new GetTransactionHistoryRecordPunchKardTask(mHandler);
				mAsyncTask.execute(VKRegApplication.uid, Constant.BOOLEAN_TRUE, pageIndexRecord,
						VKRegApplication.mPageSize);
				break;
			case RewardPunch:
				mAsyncTask = new GetTransactionHistoryRewardPunchKardTask(mHandler);
				mAsyncTask.execute(VKRegApplication.uid, pageIndexReward, VKRegApplication.mPageSize);
				break;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_GET_LIST_SUCCESS:
				switch (mScreenType) {
					case RecordPunch:
						if (!isLoadingMoreRecord) {
							listItemTransaction = (ArrayList<ItemTransactionHistory>) msg.obj;
							if (listItemTransaction == null)
								listItemTransaction = new ArrayList<ItemTransactionHistory>();

							if (listItemTransaction.size() < VKRegApplication.mPageSize)
								isNoLoadMoreRecord = true;

							if (listItemTransaction != null && listItemTransaction.size() > 0) {
								transactionAdapter = new TransactionHistoryRecordPunchAdapter(mContext,
										listItemTransaction, mHandler);
								recordPunchlistView.setAdapter(transactionAdapter);
								transactionAdapter.notifyDataSetChanged();
							}
						}
						else {
							List<ItemTransactionHistory> listResults = (List<ItemTransactionHistory>) msg.obj;
							if (listResults != null) {
								listItemTransaction.addAll(listResults);
								transactionAdapter.notifyDataSetChanged();
							}

						}

						isLoadingMoreRecord = false;
						break;
					case RewardPunch:
						if (!isLoadingMoreReward) {
							listItemRewardPunch = (ArrayList<ItemTransactionRewardPunchKard>) msg.obj;
							if (listItemRewardPunch == null)
								listItemRewardPunch = new ArrayList<ItemTransactionRewardPunchKard>();

							if (listItemRewardPunch.size() < VKRegApplication.mPageSize)
								isNoLoadMoreReward = true;

							if (pageIndexReward == 1) {
								Bundle data = msg.getData();
								if (data != null) {
									txtGiftKardRewarded
											.setText(String.valueOf(data
													.getInt(GetTransactionHistoryRewardPunchKardTask.KEY_NB_REWARDED_GIFTKARD)));
									txtGiftKardRedeemed
											.setText(String.valueOf(data
													.getInt(GetTransactionHistoryRewardPunchKardTask.KEY_NB_REDEEMED_GIFTKARD)));
								}
							}

							if (listItemRewardPunch != null && listItemRewardPunch.size() > 0) {
								mRewardPunchAdapter = new TransactionHistoryRewardPunchAdapter(mContext,
										listItemRewardPunch, mHandler);
								rewardPunchListView.setAdapter(mRewardPunchAdapter);
								mRewardPunchAdapter.notifyDataSetChanged();
							}
						}
						else {
							List<ItemTransactionRewardPunchKard> listResults = (List<ItemTransactionRewardPunchKard>) msg.obj;
							if (listResults != null) {
								listItemRewardPunch.addAll(listResults);
								mRewardPunchAdapter.notifyDataSetChanged();
							}
						}

						isLoadingMoreReward = false;
						break;
				}
				return true;
		}
		return super.handleMessage(msg);
	}

}
